package com.yinuo.common;


import com.yinuo.entity.GlobalContext;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * 本地命令工具类
 * 调用本地shell或dos命令
 * 程序自动区分系统环境
 */
public class LocalCommand {
    /*
    系统类型
     */
    public static final String WINDOWS = "windows";
    public static final String LINUX = "linux";
    /**
     * 查看"D:\"目录, Windows系统下查看目录的命令是dir
     */
    public static void checkDirectory() throws IOException {
        ProcessBuilder processBuilder = new ProcessBuilder("cmd","/c","dir");
        processBuilder.directory(new File("D:/"));
        Process process = processBuilder.start();
        BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream(), "GBK"));
        String line;
        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }
    }

    /**
     * 查看ip地址【Windows系统下】
     */
    public static void checkPhysicAddress() {
        ProcessBuilder processBuilder = new ProcessBuilder("ipconfig", "/all");
        try {
            Process process = processBuilder.start();
            BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream(), "GBK"));
            String line;
            while ((line = br.readLine()) != null) {
                if (line.indexOf("IPv4") != -1) {
                    System.out.println(line);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        List<String> cmds = new ArrayList<String>();
        /*cmds.add("dir");
        cmds.add("E:\\");*/

        //cmds.add("cd .. && dir");

        /*cmds.add("C:\\Users\\Administrator\\Desktop\\qucikly_install_jdk.bat");



        String result = execCmd(cmds);*/
        String result =
        System.getProperty("user.dir")+ GlobalContext.DOC_SUMMAY_FILES;
        //System.out.println(result);
        String path = "D:\\ideaProject\\docSummay/DocSummaryFiles/\\modular\\java\\qucikly_jdk\\qucikly_install_jdk.bat";
        cmds.add(path);
        LocalCommand.execCmd(cmds);
    }


    public static String execCmd(List<String> cmds){
        if(systemEnv().equals(LocalCommand.WINDOWS))
            return executeDos(cmds);
        return executeShell(cmds);
    }


    public static String systemEnv(){
        return System.getProperty("os.name").toLowerCase().contains(LocalCommand.WINDOWS)?LocalCommand.WINDOWS:LocalCommand.LINUX;
    }

    public static String executeShell(List<String> cmds){
        ProcessBuilder processBuilder = new ProcessBuilder();
        Process process = null;
        BufferedReader br = null;
        StringBuffer consoleResult = new StringBuffer();
        try {
            String tempStr = null;
            process = processBuilder.command(cmds).start();
            br = new BufferedReader(new InputStreamReader(process.getInputStream(), "GBK"));
            while((tempStr=br.readLine())!=null){
                consoleResult.append(tempStr).append("\n");
            }
            return consoleResult.toString();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }finally{
            try {
                if(br!=null)br.close();
                if(process!=null)process.destroy();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static String executeDos(List<String> cmds){
        List<String> cmdList = new ArrayList<>(10);
        cmdList.add("cmd.exe");
        cmdList.add("/c");
        cmdList.addAll(cmds);
        ProcessBuilder processBuilder = new ProcessBuilder(cmdList);
        Process process = null;
        BufferedReader br = null;
        StringBuffer consoleResult = new StringBuffer();
        try {
            String tempStr = null;
            process = processBuilder.start();

            br = new BufferedReader(new InputStreamReader(process.getInputStream(), "GBK"));
            while((tempStr=br.readLine())!=null){
                consoleResult.append(tempStr).append("\n");
            }
            return consoleResult.toString();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }finally{
            try {
                if(br!=null)br.close();
                if(process!=null)process.destroy();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
